package paginas;

import java.io.IOException;








import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import webservice.*;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinSession;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;


public class Login {
	
	
 	
	private TextField txtUsuario = new TextField();
	private PasswordField txtPassword = new PasswordField();
	Label lblTitulo = new Label("DeportGol");
	
		
	public TextField getTxtUsuario() {
		return txtUsuario;
	}

	public void setTxtUsuario(TextField txtUsuario) {
		this.txtUsuario = txtUsuario;
	}

	public PasswordField getTxtPassword() {
		return txtPassword;
	}

	public void setTxtPassword(PasswordField txtPassword) {
		this.txtPassword = txtPassword;
	}
	
	
	
	
	public Panel PanelLogin(){
		
		Button btnAceptar = new Button("Aceptar");
		txtUsuario.setCaption("Usuario");
		txtPassword.setCaption("Clave");
		txtUsuario.setWidth("100px");
		txtPassword.setWidth("100px");
		txtUsuario.setStyleName("txtEstilo");
		txtPassword.setStyleName("txtEstilo"); 	
		Panel panel = new Panel();
		
		panel.setHeight("150px");
		panel.setWidth("350px");
		VerticalLayout layout = new VerticalLayout();
		layout.setSizeFull();
		layout.setSpacing(true);
		layout.setStyleName("vLayout");
		HorizontalLayout hLayoutT = new HorizontalLayout();
		HorizontalLayout hLayoutB= new HorizontalLayout();
		
		hLayoutT.setSpacing(true);
		hLayoutT.addComponent(txtUsuario);
		hLayoutT.addComponent(txtPassword);
		hLayoutT.setComponentAlignment(txtUsuario, Alignment.MIDDLE_CENTER);
		hLayoutT.setComponentAlignment(txtPassword, Alignment.MIDDLE_CENTER);
		
		hLayoutB.setSpacing(true);
		hLayoutB.addComponent(btnAceptar);
		hLayoutB.setComponentAlignment(btnAceptar, Alignment.MIDDLE_CENTER);
		
		
		/*layout.setMargin(true);
		layout.addComponent(txtUsuario);
		layout.addComponent(txtPassword);
	    layout.setComponentAlignment(txtUsuario, Alignment.TOP_CENTER);
		layout.setComponentAlignment(txtPassword, Alignment.MIDDLE_CENTER);*/
		
		
		layout.addComponent(hLayoutT);
		layout.addComponent(hLayoutB);
		layout.setComponentAlignment(hLayoutT, Alignment.TOP_CENTER);
		layout.setComponentAlignment(hLayoutB, Alignment.TOP_CENTER);
		panel.setContent(layout);
		
		
		btnAceptar.addClickListener(new ClickListener() {
			
			
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				((PaginaPrincipal) UI.getCurrent()).AceptarLogin();
				
			}
		});
		
		
		
		
		return panel;
			
	}
	
	
	
	public VerticalLayout LayoutTitulo(){
		VerticalLayout layout = new VerticalLayout();
		lblTitulo.setSizeUndefined();
		lblTitulo.setStyleName("titulo");
		layout.addComponent(lblTitulo);
		layout.setComponentAlignment(lblTitulo, Alignment.MIDDLE_CENTER);
		layout.setHeight("200px");
		return layout;
	}

	
	
	
	
	public VerticalLayout Aceptar() throws IOException{
		
	
		webservice.Empresa e = new webservice.Empresa();
		e.setUsuario(txtUsuario.getValue());
		e.setClave(txtPassword.getValue());
		webservice.Empresa retorno = new webservice.Empresa();
		webservice.IWebServiceCentralService wss = new webservice.IWebServiceCentralService();
		webservice.IWebServiceCentral ws = wss.getIWebServiceCentralPort();
		retorno = ws.buscarEmpresaLogin(e);
		if (retorno != null) {

			if (retorno.isHabilitado() == true) {
		//if(txtUsuario.getValue().equals("usu")&& txtPassword.getValue().equals("pass")){
				
				VaadinSession.getCurrent().setAttribute("empresa", retorno);
				MenuPrincipal m = new MenuPrincipal();
				VerticalLayout layout = m.menu();
				txtUsuario.setValue("");
				txtPassword.setValue("");
				
				return layout;
		}else
			return null;
		}else
			return null;
		
	}
	public Panel Registro(){
				
		AltaEmpresa ae = new AltaEmpresa();
		Panel panel =ae.panelAltaEmpresa();
		panel.setWidth("400px");
		txtUsuario.setValue("");
		txtPassword.setValue("");
		return panel;
		
	}
	
	
	
	
	

}
